import { CommentEntity } from 'src/api/comment/entities/comment.entity';
import { CommentLikeEntity } from 'src/api/comment/entities/comment.like.entity';
import { SharedEntity } from 'src/api/shared/entities/shared.entity';
import { Column, Entity, Index, OneToMany } from 'typeorm';
import { UserRoleEntity } from './user.role.entity';

@Entity('user')
export class UserEntity extends SharedEntity {
  @Column({
    type: 'varchar',
    length: 30,
    name: 'username',
    comment: '用户名',
  })
  username: string;

  @Index()
  @Column({
    type: 'varchar',
    nullable: true,
    length: 11,
    name: 'mobile',
    comment: '手机号码',
  })
  mobile: string;

  @Index()
  @Column({
    type: 'varchar',
    nullable: true,
    length: 50,
    name: 'email',
    comment: '邮箱',
  })
  email: string;

  @Column({
    type: 'varchar',
    length: 100,
    name: 'password',
    nullable: true,
    select: false,
    comment: '密码',
  })
  password: string;

  @Column({
    type: 'tinyint',
    default: 0,
    name: 'is_super',
    comment: '是否为超级管理员，1表示是，0表示不是',
  })
  isSuper: number;

  @Column({
    type: 'varchar',
    name: 'avatar',
    nullable: true,
    comment: '头像',
  })
  avatar: string;

  // 一个用户对应多条评论
  @OneToMany(() => CommentEntity, (articleComment) => articleComment.user)
  articleComments: CommentEntity[];

  // 一个用户对应多条评论点赞
  @OneToMany(() => CommentLikeEntity, (articleCommentLike) => articleCommentLike.user)
  articleCommentLikes: CommentLikeEntity[];

  // 一个用户对应角色
  @OneToMany(() => UserRoleEntity, (userRole) => userRole.user, {
    cascade: true,
  })
  userRoles: UserRoleEntity[];
}
